package com.han.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doudian.open.api.token.AccessTokenData;
import com.han.core.domain.RestResult;
import com.han.core.domain.dto.QueryProductPageDTO;
import com.han.core.domain.dto.UpdateProductStatusDTO;
import com.han.core.domain.vo.ProductPageVO;
import com.han.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/product")
public class ProductController {
    @Resource
    private ProductService productService;

    /**
     * 查询当前店铺下的商品分页信息
     *
     * @param queryProductDTO see {@link com.han.core.domain.dto.QueryProductPageDTO}
     * @return 分页对象结构为：{@link com.han.core.domain.vo.ProductPageVO }
     */
    @PostMapping("/page/query")
    public RestResult<Page<ProductPageVO>> getProductPage(@RequestAttribute("accessToken") AccessTokenData accessTokenData, @RequestBody QueryProductPageDTO queryProductDTO) {
        queryProductDTO.setShopId(Long.valueOf(accessTokenData.getShopId()));
        Page<ProductPageVO> result = productService.PageByQuery(queryProductDTO);
        return RestResult.success(result);
    }

    /**
     * 更新指定商品的状态，上下架
     *
     * @param dto see {@link com.han.core.domain.dto.UpdateProductStatusDTO}
     * @return true/false
     */
    @PutMapping("/status/update")
    public RestResult updateProductStatus(@RequestAttribute("accessToken") AccessTokenData accessTokenData, @RequestBody UpdateProductStatusDTO dto) {
        JSONObject jsonObject = new JSONObject();
        //todo 调用抖店sdk
        Map<Integer, Map<Long, String>> map = productService.updateProductStatus(accessTokenData, dto);
        if (map.containsKey(0)) {
            log.warn("请查询日志，商品修改失败集合：{}", map.get(0));
            return RestResult.error("失败，请查询日志");
        }
        return RestResult.success(jsonObject);
    }


}
